/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.xy.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xy.dto.ExhibitionDTO;
import com.xy.dto.ProjectDTO;
import com.xy.entity.ProjectEntity;
import com.xy.excel.ProjectExcel;
import com.xy.mapper.ProjectMapper;
import com.xy.service.IExhibitionService;
import com.xy.service.IProjectService;
import com.xy.vo.ProjectVO;
import com.xy.wrapper.ProjectWrapper;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

/**
 * 项目表 服务实现类
 *
 * @author BladeX
 * @since 2023-07-18
 */
@Service
@AllArgsConstructor
public class ProjectServiceImpl extends BaseServiceImpl<ProjectMapper, ProjectEntity> implements IProjectService {
	private final IExhibitionService exhibitionService;

	@Override
	public IPage<ProjectVO> selectProjectPage(IPage<ProjectVO> page, ProjectVO project) {
		return page.setRecords(baseMapper.selectProjectPage(page, project));
	}


	@Override
	public List<ProjectExcel> exportProject(Wrapper<ProjectEntity> queryWrapper) {
		List<ProjectExcel> projectList = baseMapper.exportProject(queryWrapper);
		//projectList.forEach(project -> {
		//	project.setTypeName(DictCache.getValue(DictEnum.YES_NO, Project.getType()));
		//});
		return projectList;
	}

	@Override
	public ProjectVO getDetail(ProjectDTO project) {
		//判断项目id是否存在
		Assert.notNull(project.getId(), "项目不存在");
		ProjectEntity detail = baseMapper.selectOne(Condition.getQueryWrapper(project));
		ProjectVO projectVO = ProjectWrapper.build().entityVO(detail);
		//判断项目是否存在
		if(Func.isNotEmpty(projectVO)){
			//组装数据
			data(projectVO);
		}
		return projectVO;
	}

	/**
	 * 分页
	 * @param project
	 * @param query
	 * @return
	 */
	@Override
	public IPage<ProjectVO> pageProjectVO(Map<String, Object> project, Query query) {
		IPage<ProjectEntity> page = this.page(Condition.getPage(query), Condition.getQueryWrapper(project, ProjectEntity.class));
		IPage<ProjectVO> projectVOIPage = ProjectWrapper.build().pageVO(page);
		projectVOIPage.getRecords().forEach(o->{
			data(o);
		});
		return projectVOIPage;
	}

	/**
	 * 组装数据
	 * @param o
	 */
	private void  data (ProjectVO o){
		ExhibitionDTO exhibition = BeanUtil.newInstance(ExhibitionDTO.class);
		exhibition.setProjectId(o.getId());
		//查询展会数量
		Long exhibitionNum = exhibitionService.selectExhibitionNum(exhibition);
		o.setExhibitionNum(exhibitionNum);
	}
}
